package com.lysj.bhp.borrowing.controller;

import com.lysj.bhp.borrowing.base.BorrowingContent;
import com.lysj.bhp.borrowing.domain.ServiceCharge;
import com.lysj.bhp.util.web.ParamUtil;
import com.lysj.bhp.util.web.Resp;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;

/**
 * @Author: ZhangWenJian
 * @Create: 2018/8/15--10:14
 * @Description:手续费管理类
 */
@RestController
@RequestMapping("admin/service_charge")
public class ServiceChargeController extends BorrowingContent {

    @RequestMapping("/list")
    public Resp list(ServiceCharge model){
        Specification specification = ParamUtil.createSpecification(model);
        List<ServiceCharge> serviceCharges = serviceChargeRepository.findAll(specification);
        return new Resp(serviceCharges);
    }


    @PostMapping("edit")
    public Resp edit(ServiceCharge model){
        if( ParamUtil.isBlack(model.getServiceChargeId())){
            return new Resp(Resp.PARAM_ERROR,"未识别Id，重试！");
        }
        ServiceCharge serviceCharge = serviceChargeRepository.findOne(model.getServiceChargeId());
        if(serviceCharge == null){
            return new Resp(Resp.PARAM_ERROR,"数据错误！请重试");
        }
        if(model.getServiceRate() == null){
            return new Resp(Resp.PARAM_ERROR,"请输入手续费");
        }
        if( model.getServiceRate().compareTo(new BigDecimal(BigInteger.ZERO))<= 0){
            return new Resp(Resp.PARAM_ERROR,"手续费比率不能小于等于0");
        }
        serviceCharge.setServiceRate(model.getServiceRate());
        serviceChargeRepository.save(serviceCharge);
        return new Resp("操作成功！");
    }
}
